package com.hong.utils.exchanger;

import java.util.concurrent.Exchanger;

/**
 * @Auther: Administrator
 * @Date: 2018/7/22 22:40
 * @Description:
 */
public class ExchangerExample2 {
    /**
     * Actor
     * @param args
     */
    public static void main(String[] args) {
        final Exchanger<Object> exchanger = new Exchanger<Object>();
        new Thread(){
            @Override
            public void run() {
                Object aobj = new Object();
                System.out.println("A will send object "+ aobj);
                try {
                    Object robj = exchanger.exchange(aobj);
                    System.out.println("A recieved the object "+robj);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                Object aobj = new Object();
                System.out.println("B will send object "+ aobj);
                try {
                    Object robj = exchanger.exchange(aobj);
                    System.out.println("B recieved the object "+robj);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
